package com.shiroexploit.gui;

import com.shiroexploit.util.Config;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ProxyConfigPane {
    private CheckBox enableProxy = new CheckBox("启用代理");
    private TextField ipTextField = new TextField();
    private TextField portTextField = new TextField();
    private Button confirm = new Button("确定");
    private Button cancel = new Button("取消");
    private GridPane gridPane = new GridPane();
    private StartPane previous;

    public ProxyConfigPane(StartPane startPane){
        previous = startPane;
        drawPane();
        addListeners();
    }

    public Pane getPane(){
        return gridPane;
    }

    public void update(){
        enableProxy.setSelected(Config.getInstance().isProxyEnabled());
        ipTextField.setText(Config.getInstance().getProxyIP());
        portTextField.setText(String.valueOf(Config.getInstance().getProxyPort()));
    }

    private void drawPane(){
        gridPane.setPadding(new Insets(10,10,10,10));
        gridPane.setHgap(10);
        gridPane.setVgap(20);
        gridPane.setAlignment(Pos.CENTER);

        enableProxy.setSelected(false);
        gridPane.setColumnSpan(enableProxy, 2);
        gridPane.add(enableProxy, 0, 0);

        Label labelForIP = new Label("IP地址");
        ipTextField.setPrefWidth(200);
        ipTextField.setDisable(true);
        gridPane.add(labelForIP, 0, 1);
        gridPane.add(ipTextField, 1,1);

        Label labelForPort = new Label("端口");
        portTextField.setPrefWidth(200);
        portTextField.setDisable(true);
        gridPane.add(labelForPort, 0, 2);
        gridPane.add(portTextField,1 ,2);

        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(confirm, cancel);
        hBox.setMargin(confirm, new Insets(0,10,0,0));
        gridPane.setColumnSpan(hBox, 2);
        gridPane.add(hBox, 0, 3);
    }


    private void addListeners(){
        enableProxy.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if(newValue){
                    ipTextField.setDisable(false);
                    portTextField.setDisable(false);
                }else{
                    ipTextField.setDisable(true);
                    portTextField.setDisable(true);
                }
            }
        });

        confirm.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(enableProxy.isSelected()){
                    String ip = ipTextField.getText().trim();
                    String port = portTextField.getText().trim();

                    if(ip.isEmpty()) {
                        PromptMessageUI.getAlert("输入错误","IP地址不能为空");
                        return;
                    }

                    if(port.isEmpty()) {
                        PromptMessageUI.getAlert("输入错误","端口不能为空");
                        return;
                    }

                    try{
                        Integer.parseInt(port);
                    }catch(NumberFormatException e){
                        PromptMessageUI.getAlert("输入错误","端口号输入错误");
                        return;
                    }

                    try{
                        String[] parts = ip.split("\\.");
                        if(parts.length != 4){
                            PromptMessageUI.getAlert("输入错误","IP地址输入错误");
                            return;
                        }

                        for(String part : parts){
                            Integer.parseInt(part);
                        }
                    }catch(NumberFormatException e){
                        PromptMessageUI.getAlert("输入错误","IP地址输入错误");
                        return;
                    }

                    Config.getInstance().setProxyEnabled(true);
                    Config.getInstance().setProxyIP(ip);
                    Config.getInstance().setProxyPort(Integer.parseInt(port));
                }else{
                    Config.getInstance().setProxyEnabled(false);
                }

                Stage currentStage = (Stage)gridPane.getScene().getWindow();
                currentStage.hide();
                Stage pre = (Stage)previous.getPane().getScene().getWindow();
                pre.show();
            }
        });

        cancel.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage currentStage = (Stage)gridPane.getScene().getWindow();
                currentStage.hide();
                Stage pre = (Stage)previous.getPane().getScene().getWindow();
                pre.show();
            }
        });
    }
}
